﻿//Copyright (C) Microsoft Corporation.  All rights reserved.
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NorthwindDAL;
using NorthwindDAL.Repositories;
using NorthwindModel;
using NorthwindModel.Models;
using System.Collections.Generic;

namespace NorthwindTest
{
    [TestClass]
    public class CustomerRepositoryTests
    {
        private NorthwindContext context;

        [TestInitialize]
        public void Start()
        {
            context = new NorthwindContext();
        }

        [TestCleanup]
        public void Finish()
        {
            context.Dispose();
        }

        /// <summary>
        /// Querying by a valid ID should return rows from the database
        /// </summary>
        [TestMethod]
        public void GetByValidIdReturnsARow()
        {
            CustomerRepository repository = new CustomerRepository(context);
            Customer customer = repository.FindCustomerByID("ALFKI");

            Assert.IsNotNull(customer);
            Assert.AreEqual("Maria Anders", customer.ContactName);
        }

        [TestMethod]
        public void TestAddAndUpdate()
        {
            CustomerRepository repository = new CustomerRepository(context);
            Customer customer = new Customer()
            {
                CustomerID = "WOLZT",
                CompanyName = "Wolski Zajazd Test",
                ContactName = "Zbyszek Piestrzeniewicz",
                ContactTitle = "Owner",
                Address = "ul. Filtrowa 68",
                City = "Warszawa",
                Region = null,
                PostalCode = "01-012",
                Country = "Poland",
                Phone = "(26) 642-7012",
                Fax = "(26) 642-7012",
                EntityState = System.Data.EntityState.Added
            };
            repository.Save(customer);
            repository.Commit();

            customer.ContactTitle = "OwnerTest";
            repository.Save(customer);
            repository.Commit();

            Assert.IsNotNull(customer);
            Assert.AreEqual("Zbyszek Piestrzeniewicz", customer.ContactName);
        }

        [TestMethod]
        public void TestUnitOfWorkGetAndSave()
        {
            CustomerRepository repository = new CustomerRepository(context);
            List<Customer> customers = repository.FindCustomersIDContains("WOLZA");
            repository.Dispose();
            Assert.IsNotNull(customers);
            Assert.AreNotEqual(0,customers.Count);


            repository = new CustomerRepository(context);
            List<Customer> currentCustomers = repository.FindCustomersIDContains("WOLZA");
            repository.Dispose();
        }
    }
}
